#include <iostream>
#include <vector>
#include <queue>
using namespace std;
using ll=long long;
int main()
{
	int n;
	cin>>n;
	priority_queue<ll,vector<ll>,greater<ll>> q;
	for(int i=0;i<n;i++)
	{
		ll t;
		cin>>t;
		q.push(t);
	}
	ll ret=0;
	while(q.size()>=2)
	{
		ll n1=q.top();
		q.pop();
		ll n2=q.top();
		q.pop();
		ret+=n1+n2;
        q.push(n1+n2);
	}
	cout<<ret<<endl;
	return 0;
}
